﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maticsoft.Common
{
    public static class LogManager
    {
        /// <summary>
        /// 日志路径
        /// </summary>
        private static string logPath = string.Empty;

        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {
                    if (System.Web.HttpContext.Current == null)
                        // Windows Forms 应用
                        logPath = AppDomain.CurrentDomain.BaseDirectory;
                    else
                        // Web 应用
                        logPath = AppDomain.CurrentDomain.BaseDirectory + @"bin\";
                }
                return logPath;
            }
            set { logPath = value; }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string msg)
        {
            try
            {
                //路径
                string path = LogPath + "Log\\" + DateTime.Now.ToString("yyyy-MM");
                //文件名
                string file = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                //创建路径
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                using (System.IO.StreamWriter sw =new System.IO.StreamWriter(file,true))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"."+ DateTime.Now.Millisecond.ToString()+" " + msg);
                    sw.Dispose();
                }
            }
            catch
            { }
        }

    }
}
